<!-- 实际呈现效果的组件 -->
<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>▉ {{ showTitle }}</span>
      <span
        class="iconfont title-icon"
        :style="comStyle"
        @click="showChoice = !showChoice"
        >&#xe6eb;</span
      >
      <div class="select-con" :style="marginStyle" v-show="showChoice">
        <div
          class="select-item"
          v-show="item.text != showTitle"
          v-for="item in selectTypes"
          :key="item.key"
          @click="changeType(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trender_ref">seller display</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      myChart: null,
      allData: null,
      currentPage: 2,
      totalPage: 0,
      pageSize: 5,
      timerId: null,
      showChoice: false,
      choiceType: "map",
      titleFontSize: 0, // 适配字体大小
    };
  },

  computed: {
    selectTypes() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type;
      }
    },
    showTitle() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + "px",
      };
    },
  },

  created() {
    this.$socket.registerCallBack("trendData", this.getData);
  },
  mounted() {
    // 在页面查看组件结构和内容q
    window.vc = this;

    this.initChart();
    this.getData();

    // 把getData改为socket方式
    // this.$socket.send({
    //   action: "getData",
    //   socketType: "trendData",
    //   chartName: "trend",
    //   value: "",
    // });

    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unregisterCallBack("trendData");
  },

  methods: {
    //初始化echarts实例对象,要在dom结果加载完成后调用
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.trender_ref, "chalk");
      const initOption = {
        legend: {
          show: true,
          left: 20,
          top: "15%",
          icon: "circle",
        },
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
      };
      this.myChart.setOption(initOption);
    },
    // 获取数据
    // retData是服务器传回来的数据
    async getData() {
      // await this.$http.get()
      const { data: ret } = await this.$http.get("trend");
      // console.log(ret);
      // 对allData进行赋值
      this.allData = ret;
      console.log("getData running......");

      this.updateChart();
    },

    // 更新图表
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];

      // 类目轴的数据
      const timeArr = this.allData.common.month;

      // Y轴的series下的数据
      const valueArr = this.allData[this.choiceType].data;

      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: true,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });

      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        series: seriesArr,
      };
      this.myChart.setOption(dataOption);
    },

    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      this.titleFontSize = (this.$refs.trender_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };

      this.myChart.setOption(adapterOption);

      // 收到调用resize才能产生效果
      this.myChart.resize();
    },

    changeType(select) {
      this.choiceType = select;
      this.updateChart();
      this.showChoice = false;
    },
  },
};
</script>
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
}
</style>
